<template>
  <div id="details">
    <detail-nav class="detailNav" @tabClick="tabCLick" ref="nav"> </detail-nav>
    <scroll class="DetScroll" ref="scroll" :prob="3" @scroll="contentScroll">
      <details-swiper :top-images="topImages" v-if="topImages"></details-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info
        :param-info="paramInfo"
        ref="params"
      ></detail-param-info>
      <detail-comment-info
        :comment-info="commentInfo"
        ref="comment"
      ></detail-comment-info>
      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <back-top @click.native="backCLick" v-show="isShow"></back-top>
    <detail-bottom @addCart="addToCart"></detail-bottom>
  </div>
</template>

<script>
import { debounce } from "common/utils";
import Scroll from "components/common/scroll/Scroll.vue";
import { itemListemer } from "common/mixin";

import DetailNav from "./childComp/DetailNav";
import DetailsSwiper from "./childComp/DetailsSwiper";
import DetailBaseInfo from "./childComp/DetailBaseInfo";
import DetailShopInfo from "./childComp/DetailShopInfo";
import DetailParamInfo from "./childComp/DetailParamInfo";
import DetailGoodsInfo from "./childComp/DetailGoodsInfo";
import DetailCommentInfo from "./childComp/DetailCommentInfo";
import DetailBottom from "./childComp/DetailBottom";

import GoodsList from "components/content/goods/GoodsList.vue";
import BackTop from "components/content/backTop/BackTop.vue";

import {
  getDetailsById,
  Goods,
  Shop,
  GoodsParam,
  getCommendGoods,
} from "network/details";

export default {
  name: "Details",
  data() {
    return {
      iid: null,
      topImages: null,
      goods: {},
      shop: {},
      paramInfo: {},
      detailInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      themeTopY: null,
      currentIndex: 0,
      isShow: false,
    };
  },

  components: {
    Scroll,
    DetailNav,
    DetailsSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailParamInfo,
    DetailGoodsInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottom,
    BackTop,
  },
  created() {
    this.iid = this.$route.params.iid;
    this.getDetailsById(this.iid);
    this.getCommendGoods();
    this.themeTopY = debounce(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);
    });
  },
  methods: {
    tabCLick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 300);
    },
    contentScroll(position) {
      let positionY = -position.y;

      let length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex !== i &&
          i < length - 1 &&
          positionY >= this.themeTopYs[i] &&
          positionY <= this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      this.isShow = -position.y > 1500 ? true : false;
    },
    imageLoad() {
      this.$refs.scroll.refresh();
      this.themeTopY();
    },
    async getDetailsById(iid) {
      let res = await getDetailsById(iid);
      const data = res.result;
      this.topImages = res.result.itemInfo.topImages;
      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 3.创建店铺信息的对象
      this.shop = new Shop(data.shopInfo);
      // 4.保存商品的详情数据
      this.detailInfo = data.detailInfo;

      // 5.获取参数的信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      // 获取评论数据
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0] || {};
      }
    },
    async getCommendGoods() {
      let res = await getCommendGoods();
      this.recommends = res.data.list;
    },
    backCLick() {
      this.$refs.scroll.scrollTo(0, 0, 300);
    },
    addToCart(){
      let product={};
      product.image=this.topImages[0];
      product.title=this.goods.title;
      product.desc=this.goods.desc;
      product.price=this.goods.realPrice;
      product.iid=this.iid;
      this.$store.dispatch("addCart",product).then((res)=>{
      this.$toast.show(res,2000)
      })
    }
  },

  mixins: [itemListemer],
  destroyed() {
    this.$bus.$off("imageItemLoad", this.ItemListener);
  },
};
</script>

<style scoped>
#details {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.detailNav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
.DetScroll {
  height: calc(100% - 44px);
}
</style>